﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;

namespace Lswweb.Domain
{
    public class MapNode
    {
        private string name;
        private string parentName;
        private string url;
        private string urlPattern;
        private string text;
        private string path;
        private string image;
        private string imageAction;
        private bool visible = true;
        private MapType type;

        public MapNode() { }
        public MapNode(MapType type,string name,string parentName, string url,string urlPattern ,string text, string path, string image, string imageAction, bool visible)
        {
            this.type = type;
            this.name = name;
            this.parentName = parentName;
            this.url = url;
            this.urlPattern = urlPattern;
            this.text = text;
            this.path = path;
            this.image = image;
            this.imageAction = imageAction;
            this.visible = visible;
        }

        public MapType Type 
        {
            get { return this.type; }
        }

        public string Name
        {
            get { return name; }
        }
        public string Path
        {
            get { return path; }
        }

        public string Url
        {
            get { return url; }
        }

        public string Text
        {
            get { return text; }
        }

        public string Image
        {
            get { return image; }
        }

        public string ImageAction
        {
            get { return string.IsNullOrEmpty(this.imageAction) ? this.image : this.imageAction; }
        }

        public bool Visible
        {
            get { return this.visible; }
        }

        public string ParentName 
        {  
            get{ return this.parentName;}
        }

        private int _depth = -1;
        public int Depth
        {
            get
            {
                if (_depth == -1 && !string.IsNullOrEmpty(path))
                    _depth = path.Split('/').Length;
                return _depth;
            }
        }

        public bool IsMatch(string url)
        {
            return string.IsNullOrEmpty(this.urlPattern)
                ? url == this.Url
                : Regex.IsMatch(url, this.urlPattern, RegexOptions.IgnoreCase);
        }

        #region override Equals and GetHashCode

        public override bool Equals(object obj)
        {
            if (obj == null && this == null)
                return true;

            if (obj == null || this == null)
                return false;

            if (obj.GetType() != this.GetType())
                return false;

            return ((MapNode)obj).Name == this.Name;
        }

        public override int GetHashCode()
        {
            return name.GetHashCode();
        }

        #endregion
    }

    public enum MapType
    {
        AdminMenu = 1,
        Gloabls = 2
    }
}
